package com.fruitshop.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fruitshop.entity.ProductDetail;
import com.fruitshop.mapper.ProductDetailMapper;
import com.fruitshop.service.ProductDetailService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

/**
 * 商品详情服务实现类
 */
@Service
public class ProductDetailServiceImpl extends ServiceImpl<ProductDetailMapper, ProductDetail> implements ProductDetailService {

    @Override
    public ProductDetail getByProductId(Integer productId) {
        LambdaQueryWrapper<ProductDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductDetail::getProductId, productId)
               .eq(ProductDetail::getDeleted, 0);
        return getOne(wrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdateDetail(ProductDetail productDetail) {
        if (productDetail.getDetailId() == null) {
            productDetail.setCreateTime(LocalDateTime.now());
            productDetail.setDeleted(0);
        }
        productDetail.setUpdateTime(LocalDateTime.now());
        return saveOrUpdate(productDetail);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteByProductId(Integer productId) {
        LambdaQueryWrapper<ProductDetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ProductDetail::getProductId, productId);
        
        ProductDetail productDetail = new ProductDetail();
        productDetail.setDeleted(1);
        productDetail.setUpdateTime(LocalDateTime.now());
        
        return update(productDetail, wrapper);
    }
} 